package com.ruoyi.web.controller.lingshui.decisupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.supervise.domain.decisupport.EchartEntity;
import com.ruoyi.supervise.domain.decisupport.MyBigDataAllProHoursPro;
import com.ruoyi.supervise.domain.decisupport.MyBigDataProZS;
import com.ruoyi.supervise.domain.decisupport.ProportionOfProjectType;
import com.ruoyi.supervise.service.decisupport.MyBigDataAllProHoursProService;
import com.ruoyi.supervise.service.decisupport.MyBigDataProZSService;
import com.ruoyi.supervise.service.decisupport.ProportionOfProjectTypeService;
import com.ruoyi.supervise.util.DateUtils;

import cn.hutool.core.date.DateUtil;

/**
 * 
 * @ClassName: IndexController
 * @Description: 房建工程开工情况controller
 * @Author wtj
 * @DateTime 2020年1月3日 下午5:17:22
 */

@Controller
@RequestMapping("/houseconstruction")
public class HouseConstructionController {
	
	
	@Autowired
	private MyBigDataAllProHoursProService myBigDataAllProHoursProService;
	
	@Autowired
	private ProportionOfProjectTypeService proportionOfProjectTypeService;
	
	@Autowired
	private MyBigDataProZSService myBigDataProZSService;
	
	/**
	 * @Title: hello
	 * @Description: 房建工程开工情况
	 * @Author wtj
	 * @DateTime 2020年1月8日 下午2:53:42
	 * @param model
	 * @param bd
	 * @return
	 */
	@GetMapping("/analysis")
	public String hello() {
//		bd.setTimeNowBegin("2019-1-01");
//		bd.setTimeNowEnd("2019-12-31");
//		bd.setTimeLastBegin("2018-01-01");
//		bd.setTimeLastEnd("2018-12-31");
		
//		bd.setTimeNowBegin("2019-01");
//		bd.setTimeNowEnd("2019-12");
//		bd.setTimeLastBegin("2018-01");
//		bd.setTimeLastEnd("2018-12");
//		bd.setIsPrivateConstruction("1");
		
		return "decisionSupport/houseConstruction/page.html";
	}
	
	/**
	 * @Title: indexZJTable
	 * @Description: 房建详细数据table
	 * @Author wtj
	 * @DateTime 2020年1月10日 下午2:20:35
	 * @param bd
	 * @return
	 */
	@GetMapping("/indexzjtable")
	@ResponseBody
	public List<MyBigDataAllProHoursPro> indexZJTable(MyBigDataAllProHoursPro bd) {
		MyBigDataAllProHoursPro aphp=processingTime(bd);
		//获取出中间表格的数据和左上角房建项目总览数据
		List<MyBigDataAllProHoursPro> allFJ=myBigDataAllProHoursProService.selectAllProAndEB(aphp);
		MyBigDataAllProHoursPro allProProvince=myBigDataAllProHoursProService.selectAllProAndEBProvince(aphp);
		allProProvince.setProjectCityNum("全省");
		//allFJ.add(allProProvince);
		List<MyBigDataAllProHoursPro> allFJTable=new ArrayList<MyBigDataAllProHoursPro>();
		for(int i=0;i<allFJ.size()+1;i++)
		{
			if(i==0)
			{
				allFJTable.add(allProProvince);
			}else
			{
				allFJTable.add(allFJ.get(i-1));
			}
		}
//		JSONArray json=JSONUtil.parseArray(allFJ);
//		System.out.println(json);
		return allFJTable;
	}
	
	/**
	 * @Title: compareProArea
	 * @Description: 房建工程规模占比
	 * @Author wtj
	 * @DateTime 2020年1月10日 下午2:21:18
	 * @param bd
	 * @return
	 */
	@GetMapping("/compareproarea")
	@ResponseBody
	public Map<String,Object> compareProArea(MyBigDataAllProHoursPro bd) {
		MyBigDataAllProHoursPro aphp=processingTime(bd);
		aphp.setProjectCityNum("陵水县");
		//获取右下角房建工程规模占比数据
		int inOneW=myBigDataAllProHoursProService.countFJAreaInOneW(aphp);
		int oneWToTwoW=myBigDataAllProHoursProService.countFJAreaOneWToTwoW(aphp);
		int twoWToFiveW=myBigDataAllProHoursProService.countFJAreaTwoWToFiveW(aphp);
		int fiveWToTenW=myBigDataAllProHoursProService.countFJAreaFiveWToTenW(aphp);
		int upTenW=myBigDataAllProHoursProService.countFJAreaUpTenW(aphp);
		Map<String,Object> compareProAreaMap=new HashMap<String,Object>();
		String[] strArr= {"1万平米以内","1-2万平米","2-5万平米","5-10万平米","10万平米以上"};
		int[] iArr= {inOneW,oneWToTwoW,twoWToFiveW,fiveWToTenW,upTenW};
		compareProAreaMap.put("y", strArr);
		compareProAreaMap.put("x", iArr);
		return compareProAreaMap;
	}
	
	/**
	 * @Title: compareProType
	 * @Description: 各类型房建项目占比
	 * @Author wtj
	 * @DateTime 2020年1月10日 下午2:22:06
	 * @param bd
	 * @return
	 */
	@GetMapping("/compareprotype")
	@ResponseBody
	public List<ProportionOfProjectType> compareProType(MyBigDataAllProHoursPro bd) {
		MyBigDataAllProHoursPro aphp=processingTime(bd);
		aphp.setProjectCityNum("陵水县");
		List<ProportionOfProjectType> pptList=proportionOfProjectTypeService.FJProportionOfProjectTypeBT(aphp);
		return pptList;
	}
	
	/**
	 * @Title: startNumRLTFJ
	 * @Description: 开工数量热力图
	 * @Author wtj
	 * @DateTime 2020年1月10日 下午2:22:40
	 * @param bd
	 * @return
	 */
	@GetMapping("/startnumrltfj")
	@ResponseBody
	public List<EchartEntity> startNumRLTFJ(MyBigDataAllProHoursPro bd) {
		MyBigDataAllProHoursPro aphp=processingTime(bd);
		List<MyBigDataAllProHoursPro> allRENumData=myBigDataAllProHoursProService.selectAllNumRERLT(aphp);
		
		String yp="0";
		for(MyBigDataAllProHoursPro mdapp:allRENumData)
		{
			if(mdapp.getProjectCityNumRE().equals("洋浦经济开发区"))
			{
				yp=mdapp.getTotalNum();
			}
		}
		for(MyBigDataAllProHoursPro mdapp:allRENumData)
		{
			if(mdapp.getProjectCityNumRE().equals("儋州市"))
			{
				int dzLastInt=Integer.parseInt(mdapp.getTotalNum());
				int ypInt=Integer.parseInt(yp);
				int dzNowInt=dzLastInt+ypInt;
				mdapp.setTotalNum(String.valueOf(dzNowInt));
			}
		}
		List<EchartEntity> eeNumList=new ArrayList<EchartEntity>();
		for(MyBigDataAllProHoursPro m:allRENumData)
		{
			EchartEntity ee=new EchartEntity();
			ee.setName(m.getProjectCityNumRE());
			ee.setValue(m.getTotalNum());
			eeNumList.add(ee);
		}
		return eeNumList;
	}
	
	/**
	 * @Title: startAreaRLTFJ
	 * @Description: 开工面积热力图
	 * @Author wtj
	 * @DateTime 2020年1月10日 下午2:23:15
	 * @param bd
	 * @return
	 */
	@GetMapping("/startarearltfj")
	@ResponseBody
	public List<EchartEntity> startAreaRLTFJ(MyBigDataAllProHoursPro bd) {
		MyBigDataAllProHoursPro aphp=processingTime(bd);
		List<MyBigDataAllProHoursPro> allREAreaData=myBigDataAllProHoursProService.selectAllAreaRERLT(aphp);
		String yp="0";
		for(MyBigDataAllProHoursPro mdapp:allREAreaData)
		{
			if(mdapp.getProjectCityNumRE().equals("洋浦经济开发区"))
			{
				yp=mdapp.getTotalAreaRE();
			}
		}
		for(MyBigDataAllProHoursPro mdapp:allREAreaData)
		{
			if(mdapp.getProjectCityNumRE().equals("儋州市"))
			{
				Double dzLastDou=Double.parseDouble(mdapp.getTotalAreaRE());
				Double ypDou=Double.parseDouble(yp);
				Double dzNowDou=dzLastDou+ypDou;
				mdapp.setTotalAreaRE(String.valueOf(dzNowDou));
			}
		}
		List<EchartEntity> eeAreaList=new ArrayList<EchartEntity>();
		for(MyBigDataAllProHoursPro m:allREAreaData)
		{
			EchartEntity ee=new EchartEntity();
			ee.setName(m.getProjectCityNumRE());
			ee.setValue(m.getTotalAreaRE());
			eeAreaList.add(ee);
		}
		return eeAreaList;
	}
	
	/**
	 * @Title: startAllMonthZST
	 * @Description: 全省开工月度趋势
	 * @Author wtj
	 * @DateTime 2020年1月10日 下午2:23:39
	 * @param bd
	 * @return
	 */
	@GetMapping("/startallmonthzst")
	@ResponseBody
	public Map<String,Object> startAllMonthZST(MyBigDataAllProHoursPro bd) {
		MyBigDataAllProHoursPro aphp=processingTime(bd);
		aphp.setProjectCityNum("陵水县");
		List<MyBigDataProZS> allProvinceStartProList=myBigDataProZSService.selectAllProMonthZSData(aphp);
		Map<String,Object> allProvinceStartProMap=new HashMap<String,Object>();
		String[] monthArr=new String[allProvinceStartProList.size()];
		String[] areaTotalArr=new String[allProvinceStartProList.size()];
		String[] areaTotalCompareArr=new String[allProvinceStartProList.size()];
		for(int i=0;i<allProvinceStartProList.size();i++)
		{
			monthArr[i] = String.valueOf(allProvinceStartProList.get(i).getMonth());
			areaTotalArr[i]=allProvinceStartProList.get(i).getTotal();
			areaTotalCompareArr[i]=allProvinceStartProList.get(i).getCompare();
		}
		allProvinceStartProMap.put("month", monthArr);
		allProvinceStartProMap.put("areaTotalArr", areaTotalArr);
		allProvinceStartProMap.put("areaTotalCompareArr", areaTotalCompareArr);
		return allProvinceStartProMap;
	}
	
	/**
	 * @Title: startFJMonthZST
	 * @Description: 房地产开工月度趋势
	 * @Author wtj
	 * @DateTime 2020年1月10日 下午2:23:59
	 * @param bd
	 * @return
	 */
	@GetMapping("/startfjmonthzst")
	@ResponseBody
	public Map<String,Object> startFJMonthZST(MyBigDataAllProHoursPro bd) {
		MyBigDataAllProHoursPro aphp=processingTime(bd);
		aphp.setProjectCityNum("陵水县");
		List<MyBigDataProZS> allProvinceFJStartProList=myBigDataProZSService.selectAllFJMonthZSData(aphp);
		Map<String,Object> allProvinceFJStartProMap=new HashMap<String,Object>();
		String[] monthArr=new String[allProvinceFJStartProList.size()];
		String[] areaTotalArr=new String[allProvinceFJStartProList.size()];
		String[] areaTotalCompareArr=new String[allProvinceFJStartProList.size()];
		for(int i=0;i<allProvinceFJStartProList.size();i++)
		{
			monthArr[i] = String.valueOf(allProvinceFJStartProList.get(i).getMonth());
			areaTotalArr[i]=allProvinceFJStartProList.get(i).getTotal();
			areaTotalCompareArr[i]=allProvinceFJStartProList.get(i).getCompare();
		}
		allProvinceFJStartProMap.put("month", monthArr);
		allProvinceFJStartProMap.put("areaTotalArr", areaTotalArr);
		allProvinceFJStartProMap.put("areaTotalCompareArr", areaTotalCompareArr);
		return allProvinceFJStartProMap;
	}
	
	@GetMapping("/hainan.json")
	public String haiNanJson(Model model,MyBigDataAllProHoursPro bd) {
		return "decisionSupport/houseConstruction/hainan.json";
	}
	
	public MyBigDataAllProHoursPro processingTime(MyBigDataAllProHoursPro bd)
	{
		//对时间进行处理
		DateUtils du=new DateUtils();
		Calendar calendarBegin = Calendar.getInstance();
		Calendar calendarEnd = Calendar.getInstance();
		if(StringUtils.isAnyBlank(bd.getTimeNowBegin(),bd.getTimeNowEnd()))
		{
			Date beginDayOfNowYear=du.getBeginDayOfYear();//获取当前年的第一天
			Date endDayOfNowYear=du.getEndDayOfYear();//获取当前年的最后一天
			calendarBegin.setTime(beginDayOfNowYear);
			calendarBegin.add(Calendar.YEAR,-1);
			Date beginDayOfLastYear=calendarBegin.getTime();//获取特定时间的去年时间
			calendarEnd.setTime(endDayOfNowYear);
			calendarEnd.add(Calendar.YEAR,-1);
			Date endDayOfLastYear=calendarEnd.getTime();//获取特定时间的去年时间
			SimpleDateFormat formatterNow2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			bd.setTimeNowBegin(formatterNow2.format(beginDayOfNowYear));
			bd.setTimeNowEnd(formatterNow2.format(endDayOfNowYear));
			bd.setTimeLastBegin(formatterNow2.format(beginDayOfLastYear));
			bd.setTimeLastEnd(formatterNow2.format(endDayOfLastYear));
		}else
		{	
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date dateNowBegin = DateUtil.parse(bd.getTimeNowBegin(), "yyyy-MM");
			Date dateNowBeginDay=DateUtil.beginOfMonth(dateNowBegin);
			bd.setTimeNowBegin(formatter.format(DateUtil.beginOfMonth(dateNowBeginDay)));
			calendarBegin.setTime(dateNowBeginDay);
			calendarBegin.add(Calendar.YEAR,-1);
			Date beginDayOfLastYearMonth=calendarBegin.getTime();
			bd.setTimeLastBegin(formatter.format(DateUtil.beginOfMonth(beginDayOfLastYearMonth)));
			Date dateNowEnd = DateUtil.parse(bd.getTimeNowEnd(), "yyyy-MM");
			Date dateNowEndDay=DateUtil.beginOfMonth(dateNowEnd);
			bd.setTimeNowEnd(formatter.format(DateUtil.endOfMonth(dateNowEndDay)));
			calendarBegin.setTime(dateNowEndDay);
			calendarBegin.add(Calendar.YEAR,-1);
			Date EndDayOfLastYearMonth=calendarBegin.getTime();
			bd.setTimeLastEnd(formatter.format(DateUtil.endOfMonth(EndDayOfLastYearMonth)));
		}
		return bd;
	}
	
	@GetMapping("/test")
	@ResponseBody
	public List<MyBigDataAllProHoursPro> test() {
		MyBigDataAllProHoursPro m=new MyBigDataAllProHoursPro();
		String strYearBegin="2018";
		String strYearEnd="2019";
		m.setTimeNowBegin(strYearBegin+"-01-31 00:00:00");
		m.setTimeNowEnd(strYearEnd+"-12-31 23:59:59");
		m.setIsPrivateConstruction("1");
		List<MyBigDataAllProHoursPro> allREAreaData=myBigDataAllProHoursProService.selectAllAreaRERLT(m);
		return allREAreaData;
	}
}
